package com.demo.zookeeper;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class ZooKeeperUtil1 {

	public static ZooKeeper connection() throws IOException {
		ZooKeeper zk = new ZooKeeper("localhost:2181,localhost:2182,localhost:2183", 300000, new DemoWatcher());// 连接zk
		if (!zk.getState().equals(ZooKeeper.States.CONNECTED)) {
			while (true) {
				if (zk.getState().equals(ZooKeeper.States.CONNECTED)) {
					break;
				}
				try {
					TimeUnit.SECONDS.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		return zk;
	}

	public void testZkClient() {
		ZkClient zkClient = new ZkClient("localhost:2181");
		String node = "/app2";
		if (!zkClient.exists(node)) {
			zkClient.createPersistent(node, "hello zk");
		}
		System.out.println(zkClient.readData(node));
	}

	public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
		ZooKeeper zk = connection();
		String node = "/app1";
		Stat stat = zk.exists(node, false);// 检测/app1是否存在
		if (stat == null) {
			// 创建节点
			String createResult = zk.create(node, "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println(createResult);
		}
		// 获取节点的值
		byte[] b = zk.getData(node, false, stat);
		System.out.println(new String(b));
		zk.close();
	}

	static class DemoWatcher implements Watcher {
		@Override
		public void process(WatchedEvent event) {
			System.out.println("----------->");
			System.out.println("path:" + event.getPath());
			System.out.println("type:" + event.getType());
			System.out.println("stat:" + event.getState());
			System.out.println("<-----------");
		}
	}
}
